﻿module Sustainalytics.Governance {

    import Common = Sustainalytics.Common;
    import Company = Sustainalytics.Company;
    import LRTM = Sustainalytics.LRTM;
    import Research = Sustainalytics.Research;

    export class SummaryRelativePerformanceController {
        private govProfile: GovernanceProfileController;
        public companyRelativePerformance: GovernanceRelativePerformanceDto;

        public notFoundMessage: string;
        public $routeParams: any;
        public companyId:string;
        public regionalPercentileOrdinal: string;
        //private userId: string;
        //private accountId: string;

        constructor(
            private $rootScope: ng.IScope,
            protected $scope: Company.ICompanyBasedScope,
            protected $q: ng.IQService,
            protected $sce: ng.ISCEService,
            protected $timeout: ng.ITimeoutService,
            protected $location: ng.ILocationService,
            protected RoutingService: Routing.IRoutingService,
            protected CompanyService: Company.ICompanyService,
            protected GovernanceCompanyProfileService: GovernanceCompanyProfileService,
            protected LongRunningTaskManagerService: LRTM.LongRunningTaskManagerService,
            private NumberUtilityService: Common.NumberUtilityService) {

            
            var ctx = this;
            
            ctx.govProfile = this.$scope['governancectrl'];
            ctx.companyId = ctx.govProfile.company.id;
            ctx.getCompanyRelativePerformance();
        }

        private getCompanyRelativePerformance(): ng.IPromise<GovernanceRelativePerformanceDto> {
            var ctx = this;

            var promise = ctx.GovernanceCompanyProfileService.getCompanyRelativePerformance(ctx.govProfile.company.id);
            ctx.$rootScope['loading'] = true;
            promise.then(function (res: any) 
            {
                if (res.errCode != undefined) {
                    console.log(res.message);
                }
                else {
                    ctx.$timeout(function () {
                        ctx.$scope.$apply(function () {
                            ctx.companyRelativePerformance = res;
                            ctx.regionalPercentileOrdinal = ctx.NumberUtilityService.getOrdinalNameEnd(ctx.companyRelativePerformance.percentile);
                            ctx.$rootScope['loading'] = false;
                        });
                    });
                }
            }).finally(() => {
                ctx.$rootScope['loading'] = false;
            });

            return promise;
        }

        private setupState(id: string): string {
            console.log(id);
            if (this.companyId === id) {
                return 'table-cell-2 width_370 green-bold';
            }
            return 'table-cell-2 width_370';

        }
    }

    angular.module(governanceModuleName).controller('SummaryRelativePerformanceController', ['$rootScope', '$scope', '$q', '$sce', '$timeout', '$location', 'RoutingService', 'CompanyService', 'GovernanceCompanyProfileService', 'LongRunningTaskManagerService','NumberUtilityService', SummaryRelativePerformanceController]);

} 